﻿/*
 * 由SharpDevelop创建。
 * 用户： lucifer
 * 日期: 2011/3/17
 * 时间: 22:05
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;

namespace Yidu.Persister.Mapping
{
    /// <summary>
    /// 生产可能的数据库名称，从实体到数据库
    /// </summary>
    public class DbNameGuesser : IDbNameGuesser
    {

        
        public List<string> getPossibleNames(string name,List<Dictionary<string, object>> nameList){
            
            List<string> dataList = new List<string>();
            if(string.IsNullOrEmpty(name) || nameList == null || nameList.Count == 0){
                return dataList;
            }

            name = name.ToUpper();
            foreach(Dictionary<string, object> dict in nameList){
                if(name.Equals(Convert.ToString(dict[upper_real_name])) || name.Equals(Convert.ToString(dict[upper_entry_name]))){
                    dataList.Add(dict[real_name].ToString());
                }
            }
            
            return dataList;
        }
        
        public List<string> getPossibleTableNames(Type entryType)
        {
            if(entryType == null){
                return new List<string>();
            }
            return getPossibleTableNames(entryType.Name);
        }
        
        public List<string> getPossibleTableNames(string entryName)
        {
            
            if(string.IsNullOrEmpty(entryName)){
                return new List<string>();
            }
            IDao dao = DaoFactory.Instance.Dao;
            List<Dictionary<string, object>> tableNameList = dao.List(allTableSqlStr,false);
            
            
            return getPossibleNames(entryName,tableNameList);
        }
        
        public List<string> getPossibleColumnNames(string propertyName,List<Dictionary<string, object>> columnNameList)
        {
            if(string.IsNullOrEmpty(propertyName) || columnNameList == null || columnNameList.Count == 0 ){
                return new List<string>();
            }
            List<string> dataList = new List<string>();
            foreach(Dictionary<string, object> dict in columnNameList){
                if(propertyName.Equals(Convert.ToString(dict[field_real_name]),StringComparison.OrdinalIgnoreCase) || propertyName.Equals(Convert.ToString(dict[field_entry_name]),StringComparison.OrdinalIgnoreCase)){
                    dataList.Add(dict[field_real_name].ToString());
                }
            }
            return dataList;
        }
        
        private string allTableSqlStr = @"SELECT NAME real_name,  UPPER(REPLACE(NAME, '_', '')) upper_entry_name,
                                   UPPER(NAME) upper_real_name
                            FROM   SysObjects
                            WHERE  XType = 'U'";
        
        private string allTableFieldSqlStr = @"SELECT s.name real_name,
                                                       REPLACE(s.name, '_', '') entry_name,
                                                       t.name type_name,
                                                       s.length
                                                FROM   syscolumns s,
                                                       systypes t
                                                WHERE  s.id = OBJECT_ID(@tableName)
                                                       AND s.xusertype = t.xusertype";

        private readonly string upper_real_name = "upper_real_name";
        private readonly string upper_entry_name = "upper_entry_name";
        private readonly string real_name = "real_name";
        private readonly string field_real_name = "real_name";
        private readonly string field_entry_name = "entry_name";
                
        public List<Dictionary<string, object>> getTableFields(string tableName)
        {
            IDao dao = DaoFactory.Instance.Dao;
            return dao.List(allTableFieldSqlStr,false,"tableName",tableName);
        }

        public List<string> GetTablePrimaryKeyList(Type entryType)
        {
            return null;
        }
    }
}
